//
//  PopoverAnimator.swift
//  LVWeibo
//
//  Created by lv on 15/6/28.
//  Copyright © 2015年 lv. All rights reserved.
//

import UIKit

/// UIViewControllerTransitioningDelegate 提供转场动画相关的对象
/// UIViewControllerAnimatedTransitioning 提供转场动画的实现
class PopoverAnimator: NSObject,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning {
    
    // 视图显示的Frame
    var presentFrame:CGRect = CGRectZero
    
    // 是否展现标记
    var isPresented = false
    
    // MARK:- 返回负责转场的控制器对象
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        
        let pc = PopoverPresentationController(presentedViewController: presented, presentingViewController: presenting)
        pc.presentFrame = presentFrame
        return pc
    }
    
    // MARK:- 返回提供modal动画对象
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        return self
    }
    
    // MARK:- 返回提供 Dismissed 动画的对象
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        return self
    }
    
    // MARK:- 动画时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return isPresented ? 1.2 : 0.25
    }
    
    // MARK:-  转场动画实现方法
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        // 判断是展现还是取消
        if isPresented {
            // 1.展现，将toView添加到容器视图
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
            transitionContext.containerView()?.addSubview(toView)
            
            // 2.动画实现
            toView.transform = CGAffineTransformMakeScale(1.0, 0)
            toView.layer.anchorPoint = CGPointMake(0.5, 0)
            
            UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0.0, usingSpringWithDamping: 1, initialSpringVelocity: 10.0, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
                
                toView.transform = CGAffineTransformIdentity
                }, completion: { (_) -> Void in
                    // 结束动画 - 告诉系统转场动画结束
                    transitionContext.completeTransition(true)
            })
        }else {
            // 取消操作
            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
            
            // 取消展现视图动画
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            })
            
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                    // y值设置一个较小值，就可以实现动画效果
                    fromView.transform = CGAffineTransformMakeScale(0.0001, 0.0001)
                }, completion: { (_) -> Void in
                    
                    print("展现视图 \(fromView)")
                    // 删除fromView(展现视图)
                    fromView.removeFromSuperview()
                    
                    // ***动画结束告诉系统完毕
                    transitionContext.completeTransition(true)
            })
        }
    }
}

